<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .pic1 {
            position: absolute;
            width: 400px;
            height: 300px;
            background-color: pink;
        }
    </style>
</head>

<body>
    <div class="pic1"></div>
    <script>
        // 简单函数封装
        // obj代表目标对象,target代表目标位置
        function animate(obj, target) {
            obj.timer = setInterval(function() {
                if (obj.offsetLeft >= target) {
                    clearInterval(obj.timer);
                } else {
                    obj.style.left = obj.offsetLeft + 1 + 'px';
                }
            }, 3)

        }
        var pic1 = document.querySelector('.pic1');
        animate(pic1, 400)
    </script>
</body>

</html>